package no.uib.kwikktaim.gui;

import java.awt.Component;

import java.net.URL;

import javax.swing.*;

/**
 * We use our own list renderer to get those nice icons on the left side of the names
 */
 class MediaListRenderer extends JLabel implements ListCellRenderer {
     // Mime type icons
	 ImageIcon videoIcon = new ImageIcon(getClass().getResource("/no/uib/kwikktaim/img/video-x-generic.png"));
     ImageIcon audioIcon = new ImageIcon(getClass().getResource("/no/uib/kwikktaim/img/audio-x-generic.png"));


     public Component getListCellRendererComponent(
       JList list,
       Object value,            // value to display
       int index,               // cell index
       boolean isSelected,      // is the cell selected
       boolean cellHasFocus)    // the list and the cell have the focus
     {
         URL url = (URL) value;

         // Find the filename
    	 String s = url.getFile().substring(url.getFile().lastIndexOf('/') + 1);

    	 // A little space between the text and the icon
         setText(" "+s);

         // Beutify URL
         s = s.replaceAll("%20", " "); // correct space
         s = s.replaceAll("_", " "); // correct space

         if(s.toLowerCase().endsWith("mov") || s.toLowerCase().endsWith("avi") || s.toLowerCase().endsWith("mpg") || s.toLowerCase().endsWith("3gp"))
        	 setIcon(videoIcon); // Set video icon on video files
         else
        	 setIcon(audioIcon); // Set music icon on everything else

         // Set the color on selected and not selected list items
         if (isSelected) {
             setBackground(list.getSelectionBackground());
               setForeground(list.getSelectionForeground());
               setOpaque(true);
           }
         else {
               setBackground(list.getBackground());
               setForeground(list.getForeground());
               setOpaque(false);
           }

         return this;
     }

 }
